/*
 * Postmore - a pop3 supported mailing list server;
 * Copyright (C) 2010 Peng Lei, pyp125@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 *
 * You should have received a copy of the GNU Public License along with this
 * program. If not, see http://www.gnu.org/licenses/.
*/
#ifndef _UTILITIES_H_

#define _UTILITIES_H_
/* INI style configuration reading functions. 2010 August 9th, first version of libcfg. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <time.h>

#define LMAX 1024
#define SMAX 64
#define NMAX 64
#define VMAX 128

#define SECTION "[%s]"
#define FORMAT "%[^=]=%[^\n]"

FILE* _f_cfg;
int _cfg_lock;
/*
 Return value:
 both functions below return 0 when success, in cfg_get, -2 means wrong format;
*/
/* 
 cfg_init, must be called before cfg_get;
 p - path of configuration; 
*/
int cfg_init(const char* p);
/*
 three data types are accepted: int, float, char*
 s - section, n - name, o - output buffer; 
*/
int cfg_get(const char* s,const char* n,void* o);
/*
 cfg_exit should be called when the configuration will no longer be used; 
*/
int cfg_exit();

/* Log recording functions. */
char _p_log[255];
/*
 Return value:
 both functions below return 0 when success, otherwise -1 will be returned;
 */
#define LOG_FM "-%s\n%s\n"
/* Initlization; */
int pmlog_init(const char* p);
/* Log write; */
int pmlog(const char* p);
/* Linked list structure; */
typedef struct _node
{
	void* data;	/* Data field; */
	int size; /* Data size; */
	struct _node* next;	/* Next node; */
} NODE;
/*
 Linked list operations;
 first - the first node of the list;
 toadd - the new node which will be added to the list;
 todel - the node which will be deleted;
 both functions return 0 for success, or a negative number if failed.
*/
NODE* list_create(void* data,size_t size_in);
int list_add(NODE* first,void* toadd,size_t size_in);
int list_del(NODE* first,void* todel,size_t size_in);
int list_set(NODE* node,void* toset,size_t size_in);
void list_clear(NODE* first);
/* base64; */
int base64(const char* src,char* dst);

#endif
